﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace DataShow.codeBLL
{
    public class TrendBLL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        /// <param name="station"></param>
        /// <param name="scnen"></param>
        /// <param name="sendpoint"></param>
        /// <param name="product"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public TrendModel GetResponseModel(DateTime time, string station, string scnen, string sendpoint, string product, string type)
        {
            string productCode = product.Split('|')[0];
            string productName = product.Split('|')[1];
            TrendModel model = new TrendModel();
            model.name = productName;
            DataTable dt = new DataTable();
            string sapnCondition = "";
            switch (scnen)
            {
                case "3": sapnCondition = "'FD00_24','FD24_48','FD48_72'"; break;
                case "5": sapnCondition = "'FD00_24','FD24_48','FD48_72','FD72_96','FD96_120'"; break;
            }
            dt = new codeDAL.TrendProcess().GetMaxt(time, station, sapnCondition, sendpoint, productCode, type);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var values = dt.Rows[i]["value"].ToString();
                    string[] stationValues = values.Split('|');
                    var nowstation = stationValues.Where(x => x.Contains(station)).FirstOrDefault();
                    if (nowstation != null)
                    {
                        string valuess = nowstation.Split(';')[1];
                        model.data.Add(Math.Round(Convert.ToDouble(valuess), 1));
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public string FilterStation(string value,string station)
        {
            string[] stationValues = value.Split('|');
            var nowstation = stationValues.Where(x => x.Contains(station)).FirstOrDefault();
            if (nowstation != null)
            {
                string valuess = nowstation.Split(';')[1];
                return valuess;
            }else
            {
                return null;
            }
        }

    }
}